<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.GregorianCalendar" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="javax.portlet.PortletSession" %>
<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%@ page import="com.liferay.portal.kernel.util.WebKeys" %>
<%@ page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ page import="javax.portlet.PortletSession" %>
<%@ page import="slic.service.DeportistaLocalServiceUtil" %>
<%@ page import="slic.service.EscuelaLocalServiceUtil" %>
<%@ page import="slic.service.AlumnoLocalServiceUtil" %>
<%@ page import="slic.model.Deportista" %>
<%@ page import="slic.model.Escuela" %>
<%@ page import="slic.model.Alumno" %>

<portlet:defineObjects />

<%

	if (request.getParameter("requiereHabilitacion") != null && request.getParameter("requiereHabilitacion").trim().equals("true"))
		session.setAttribute("searchMember_requiereHabilitacion", "true");
	else if (request.getParameter("requiereHabilitacion") != null && request.getParameter("requiereHabilitacion").trim().equals("false"))
		session.setAttribute("searchMember_requiereHabilitacion", "false");

	if (request.getParameter("escuelaId") != null && !request.getParameter("escuelaId").trim().equals("")) {
		session.setAttribute("searchMember_escuelaId", request.getParameter("escuelaId"));
	}

	if (request.getParameter("edadMinima") != null && !request.getParameter("edadMinima").trim().equals("")) {
		session.setAttribute("searchMember_edadMinima", request.getParameter("edadMinima"));
	}

	if (request.getParameter("edadMaxima") != null && !request.getParameter("edadMaxima").trim().equals("")) {
		session.setAttribute("searchMember_edadMaxima", request.getParameter("edadMaxima"));
	}

	int anioActual = GregorianCalendar.getInstance().get(Calendar.YEAR);
	
	int anioNacimientoMinimo = anioActual - Integer.valueOf((String)session.getAttribute("searchMember_edadMinima")).intValue();
	int anioNacimientoMaximo = anioActual - Integer.valueOf((String)session.getAttribute("searchMember_edadMaxima")).intValue();
	
	String fechaNacimientoMaxima = "1/1/" + anioNacimientoMaximo;
	String fechaNacimientoMinima = "31/12/" + anioNacimientoMinimo;	
	
	if (renderRequest.getParameter("search") != null) {
		HashMap<String, String> map = new HashMap<String, String>();

		map.put("dniOrNombresOrApellido", renderRequest.getParameter("dniOrNombresOrApellido"));
		map.put("fechaNacimientoMaxima", fechaNacimientoMaxima);
		map.put("fechaNacimientoMinima", fechaNacimientoMinima);
		map.put("escuelaId", (String)session.getAttribute("searchMember_escuelaId"));

		//map.put("orderBy", "createDate");
		//map.put("orderType", "desc");

		request.setAttribute("searchMap", map);
	}

	if (request.getAttribute("searchMap") == null) {
		HashMap<String, String> map = new HashMap<String, String>();

		//map.put("orderBy", "createDate");
		//map.put("orderType", "desc");

		map.put("fechaNacimientoMaxima", fechaNacimientoMaxima);
		map.put("fechaNacimientoMinima", fechaNacimientoMinima);
		map.put("escuelaId", (String)session.getAttribute("searchMember_escuelaId"));
		
		request.setAttribute("searchMap", map);
	}

%>


<liferay-portlet:renderURL varImpl="searchURL">
	<portlet:param name="search" value="search"/>
</liferay-portlet:renderURL>

<liferay-portlet:renderURL varImpl="iteratorURL">
	<portlet:param name="jspPage" value="/html/equipos/searchMember.jsp"/>
</liferay-portlet:renderURL>


<%--h2><liferay-ui:message key="search-options" /></h2--%>

<aui:form>

	<liferay-portlet:renderURLParams varImpl="searchURL" />

	<aui:input inlineField="<%=true %>" label="" name="dniOrNombresOrApellido" type="text" size="30" />	
	<aui:button type="submit" value="search" />

	<liferay-ui:search-container iteratorURL="<%=iteratorURL %>" delta="10" emptyResultsMessage="No se encontraron personas." var="deportistaContainer">

		<liferay-ui:search-container-results>
		<%
			if (((String)session.getAttribute("searchMember_requiereHabilitacion")).equalsIgnoreCase("true")) {
				List<Deportista> listDeportista = DeportistaLocalServiceUtil.findByMap((HashMap<String, String>)request.getAttribute("searchMap"),
						deportistaContainer.getStart(), deportistaContainer.getEnd());

				Long longTotal = DeportistaLocalServiceUtil.countByMap((HashMap<String, String>)request.getAttribute("searchMap"));

				pageContext.setAttribute("results", listDeportista);
				pageContext.setAttribute("total", Integer.valueOf(longTotal.intValue()));
				pageContext.setAttribute("rowNumber", Integer.valueOf(0));
			}
			else {
				List<Alumno> listAlumno = AlumnoLocalServiceUtil.findByMap((HashMap<String, String>)request.getAttribute("searchMap"),
						deportistaContainer.getStart(), deportistaContainer.getEnd());

				Long longTotal = AlumnoLocalServiceUtil.countByMap((HashMap<String, String>)request.getAttribute("searchMap"));

				pageContext.setAttribute("results", listAlumno);
				pageContext.setAttribute("total", Integer.valueOf(longTotal.intValue()));
				pageContext.setAttribute("rowNumber", Integer.valueOf(0));
			}
		%>
		</liferay-ui:search-container-results>

		<c:if test="<%=((String)session.getAttribute(\"searchMember_requiereHabilitacion\")).equalsIgnoreCase(\"false\") %>">
			<liferay-ui:search-container-row
					className="slic.model.Alumno"
					keyProperty="alumnoId"
					modelVar="alumno" >

				<liferay-ui:search-container-column-text name="DNI" property="dni" />

				<liferay-ui:search-container-column-text name="Apellido" property="apellido" />

				<liferay-ui:search-container-column-text name="Nombre" property="nombres" />

				<liferay-ui:search-container-column-text name="Fecha Nac.">
					<fmt:formatDate value="<%= alumno.getFecha_nacimiento() %>" pattern="dd-MM-yyyy"/>
				</liferay-ui:search-container-column-text>

				<liferay-ui:search-container-column-text name="Escuela">
					<%
						Escuela escuela = EscuelaLocalServiceUtil.fetchEscuela(alumno.getEscuelaId());
						out.write(escuela.getNombre());
					%>
				</liferay-ui:search-container-column-text>

				<liferay-ui:search-container-column-text name="Asignar">
					<a href="#" onclick="closeVentana(null, '<%= String.valueOf(alumno.getAlumnoId()) %>')">Asignar</a>
				</liferay-ui:search-container-column-text>

			</liferay-ui:search-container-row>
		</c:if>
		<c:if test="<%=((String)session.getAttribute(\"searchMember_requiereHabilitacion\")).equalsIgnoreCase(\"true\") %>">
			<liferay-ui:search-container-row
					className="slic.model.Deportista"
					keyProperty="deportistaId"
					modelVar="deportista" >

				<liferay-ui:search-container-column-text name="DNI" property="dni" />

				<liferay-ui:search-container-column-text name="Apellido" property="apellido" />

				<liferay-ui:search-container-column-text name="Nombre" property="nombres" />

				<liferay-ui:search-container-column-text name="Fecha Nac.">
					<fmt:formatDate value="<%= deportista.getFecha_nacimiento() %>" pattern="dd-MM-yyyy"/>
				</liferay-ui:search-container-column-text>

				<liferay-ui:search-container-column-text name="Escuela">
					<%
						Escuela escuela = EscuelaLocalServiceUtil.fetchEscuela(deportista.getEscuelaId());
						out.write(escuela.getNombre());
					%>
				</liferay-ui:search-container-column-text>

				<liferay-ui:search-container-column-text name="Asignar">
					<a href="#" onclick="closeVentana('<%= String.valueOf(deportista.getDeportistaId()) %>', null)">Asignar</a>
				</liferay-ui:search-container-column-text>

			</liferay-ui:search-container-row>
		</c:if>

		<liferay-ui:search-iterator searchContainer="<%= deportistaContainer %>" />

	</liferay-ui:search-container>

</aui:form>

<aui:script>

function closeVentana(deportistaId, alumnoId) {
	Liferay.Util.getOpener().closePopUpAndRefreshPortlet('<%=liferayPortletResponse.getNamespace() + "editAsset"%>', deportistaId, alumnoId);
}

</aui:script>
